﻿'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' A big thank you to Bruce Chapman from iFinity who provided this helper class
'
' http://www.ifinity.com.au/Blog/EntryId/75/Include-jQuery-in-a-DotNetNuke-Module-with-version-independent-code
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Namespace dnnWerk.Modules.Nuntio.Content

    Public Class UIHelper

        '''<summary>
        ''' Includes the jQuery libraries onto the page
        ''' </summary>
        ''' <param name="page" />Page object from calling page/control</param>
        ''' <param name="includejQueryUI" />if true, includes the jQuery UI libraries</param>
        ''' <param name="uncompressed" />if true, includes the uncompressed libraries</param>
        ''' <param name="includeNoConflict" />if true, includes the uncompressed libraries</param>
        Friend Shared Sub InjectjQueryLibary(ByVal page As System.Web.UI.Page, ByVal includejQueryUI As Boolean, ByVal uncompressed As Boolean, ByVal includeNoConflict As Boolean)
            Dim major As Integer, minor As Integer, build As Integer, revision As Integer
            Dim injectLib As Boolean = False
            If SafeDNNVersion(major, minor, revision, build) Then
                Select Case major
                    Case 5
                        'todo: all versions of 5?
                        injectLib = False
                        Exit Select
                    Case Else
                        injectLib = True
                        Exit Select
                End Select
            Else
                injectLib = True
            End If

            If injectLib Then
                'no in-built jQuery libraries into the framework, so include the google version
                Dim [lib] As String = Nothing
                If uncompressed Then
                    [lib] = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"
                Else
                    [lib] = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
                End If

                If page.Header.FindControl("jquery") Is Nothing Then
                    Dim jQueryLib As New System.Web.UI.HtmlControls.HtmlGenericControl("script")
                    jQueryLib.Attributes.Add("src", [lib])
                    jQueryLib.Attributes.Add("type", "text/javascript")
                    jQueryLib.ID = "jquery"
                    page.Header.Controls.Add(jQueryLib)

                    If includeNoConflict Then
                        ' use the noConflict (stops use of $) due to the use of prototype 
                        ' with a standard DNN distro
                        Dim noConflictScript As New System.Web.UI.HtmlControls.HtmlGenericControl("script")
                        noConflictScript.InnerText = " jQuery.noConflict(); "
                        noConflictScript.Attributes.Add("type", "text/javascript")
                        page.Header.Controls.Add(noConflictScript)
                    End If
                End If
            Else
                'call DotNetNuke.Framework.jQuery.RequestRegistration();
                Dim jQueryType As Type = Type.[GetType]("DotNetNuke.Framework.jQuery, DotNetNuke")
                If jQueryType IsNot Nothing Then
                    'run the DNN 5.0 specific jQuery registration code
                    jQueryType.InvokeMember("RequestRegistration", System.Reflection.BindingFlags.InvokeMethod Or System.Reflection.BindingFlags.[Public] Or System.Reflection.BindingFlags.[Static], Nothing, jQueryType, Nothing)
                End If
            End If

            'include the UI libraries??
            If includejQueryUI Then
                Dim [lib] As String = Nothing
                If uncompressed Then
                    [lib] = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.js "
                Else

                    [lib] = "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js "
                End If
                page.ClientScript.RegisterClientScriptInclude("jqueryUI", [lib])
            End If
        End Sub
        ''' <summary>Helper function to insert a given javascript file into the page header</summary>
        ''' <param name="page" />Current Page object</param>
        ''' <param name="id" />Unique (for the page) id of the file</param>
        ''' <param name="src" />Qualified location of the script file</param>
        Friend Shared Sub IncludeScriptFile(ByVal page As Page, ByVal id As String, ByVal src As String)
            page.ClientScript.RegisterClientScriptInclude(id, src)
        End Sub
        ''' <summary>
        ''' Helper function to insert a given CSS file into the page header
        ''' </summary>
        ''' <param name="page" />Current Page Object</param>
        ''' <param name="id" />Unique (for the page) id of the file</param>
        ''' <param name="href" />Qualified location of the CSS file</param>
        Friend Shared Sub IncludeCSSFile(ByVal page As Page, ByVal id As String, ByVal href As String)
            Dim cssLink As HtmlLink = DirectCast(page.Header.FindControl(id), HtmlLink)
            If cssLink Is Nothing Then
                cssLink = New HtmlLink()
                cssLink.Href = href
                cssLink.Attributes.Add("rel", "stylesheet")
                cssLink.Attributes.Add("type", "text/css")
                cssLink.ID = id
                page.Header.Controls.Add(cssLink)
            End If
        End Sub
        ''' <summary>
        ''' Returns a version-safe set of version numbers for DNN
        ''' </summary>
        ''' <param name="major" />out value of major version (ie, 4,5 etc)</param>
        ''' <param name="minor" />out value of minor version (ie 1 in 5.1 etc)</param>
        ''' <param name="revision" />out value of revision (ie 3 in 5.1.3)</param>
        ''' <param name="build" />out value of build number</param>
        ''' <remarks>Dnn moved the version number during about the 4.9 version, 
        ''' which to me was a bit frustrating and caused the need for this reflection method call</remarks>
        ''' <returns>true if successful, false if not</returns>
        Friend Shared Function SafeDNNVersion(ByRef major As Integer, ByRef minor As Integer, ByRef revision As Integer, ByRef build As Integer) As Boolean
            Dim ver As System.Version = System.Reflection.Assembly.GetAssembly(GetType(DotNetNuke.Common.Globals)).GetName().Version
            If ver IsNot Nothing Then
                major = ver.Major
                minor = ver.Minor
                build = ver.Build
                revision = ver.Revision
                Return True
            Else
                major = 0
                minor = 0
                build = 0
                revision = 0
                Return False
            End If
        End Function


    End Class

End Namespace

